//
//  TimePickerView.m
//  BigToBig
//
//  Created by yi liu on 16/8/2.
//  Copyright © 2016年 cn.cloudtp. All rights reserved.
//

#import "TimePickerView.h"

@interface TimePickerView ()
<
    UIPickerViewDelegate,
    UIPickerViewDataSource
>
@property (nonatomic , strong)UIPickerView *hourPicker;
@property (nonatomic , strong)UIView *mask;
@end


@implementation TimePickerView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{
    self.mask = [[UIView alloc]initWithFrame:self.bounds];
    _mask.backgroundColor = [UIColor blackColor];
    _mask.alpha = 0.3;
    [self addSubview:_mask];
    [self addTap];
    
    UILabel *hourLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, kScreenHeight - 200, kScreenWidth / 2, 50)];
    hourLabel.text = @"时(hour)";
    hourLabel.backgroundColor = [UIColor whiteColor];
    [self addSubview:hourLabel];
    hourLabel.textAlignment = NSTextAlignmentCenter;
    
    UILabel *minuteLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth / 2, kScreenHeight - 200, kScreenWidth / 2, 50)];
    minuteLabel.text = @"分(minute)";
    [self addSubview:minuteLabel];
    minuteLabel.backgroundColor = [UIColor whiteColor];
    minuteLabel.textAlignment = NSTextAlignmentCenter;
    
    self.hourPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, kScreenHeight - 150, kScreenWidth , 150)];
    self.hourPicker.backgroundColor = [UIColor whiteColor];
    [self.hourPicker selectRow:0 inComponent:0 animated:YES];
    [self.hourPicker selectRow:0 inComponent:1 animated:YES];
    [self addSubview:self.hourPicker];
    _hourPicker.delegate = self;
    _hourPicker.dataSource = self;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 20;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return 24;
    }
    else{
        return 12;
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        NSString *string = [NSString stringWithFormat:@"%ld",row];
        return string;
    }
    else{
        NSString *string = [NSString stringWithFormat:@"%ld",row * 5];
        return string;
    }
}


- (void)hide
{
    [UIView animateWithDuration:0.5 animations:^(void) {
        self.transform = CGAffineTransformTranslate(self.transform, 0,  self.frame.size.height);
        self.mask.alpha = 0;
        [self.mask removeFromSuperview];
    } completion:^(BOOL isFinished) {
        [self removeFromSuperview];
        if (_isDuration) {
            [self.timePickerDelegate refreshRoadShowDurationWithDuration:[self.hourPicker selectedRowInComponent:0] * 60 + [self.hourPicker selectedRowInComponent:1] * 5];
        }else{
            [self.timePickerDelegate refreshTableViewWithHour:[self.hourPicker selectedRowInComponent:0] Minute:[self.hourPicker selectedRowInComponent:1] * 5];

        }
    }];
}


- (void)addTap
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [self.mask addGestureRecognizer:tap];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
